<?php
error_reporting(0);
?>

<div align="left"><p><?=translate('warning_note')?></p><p><b><?=translate('view_clients');?></b></p><br></div>
<?php
$select_user=translate('client');
#$message_upgr_success=translate('message_upgr_success');
$no_clients=translate('empty_list');
$no_client_selected=translate('no_client_selected');
echo "<script language='JavaScript'>
    <!--
    var imgDir = 'images/';
    var preloadImages = new Array();
    preloadImages[0] = new Image();
    preloadImages[0].src = imgDir + 'spacer.gif';
    preloadImages[1] = new Image();
    preloadImages[1].src = imgDir + 'callout_a_1.gif';
    preloadImages[2] = new Image();
    preloadImages[2].src = imgDir + 'callout_a_2.gif';
    preloadImages[3] = new Image();
    preloadImages[3].src = imgDir + 'callout_a_3.gif';
    preloadImages[4] = new Image();
    preloadImages[4].src = imgDir + 'callout_a_4.gif';
   	//-->
</script>";
$params[sid] = $sid;
$params[action] = "viewClients";
$url = "$baseURL/$webDIR/?".encode_params($params);
include('wallet_db.html');
//-----------------------------------------------------
	$sql['type'] = "SELECT";
	$sql['params'] = "client_id, username, blocked";
	$sql['table'] = "clients";
	$sql['where'] = "reseller_id='$id'";
	$sql['options'] = "ORDER BY username";
	$sql['return'] = "struct";
//------------------------------------------------------
if (!$result=sql_exec($sql,$dbh81,'assoc')) {
	exit("<b>$no_clients</b>");
}
foreach ($result as $value) {
	$clients[$value[client_id]]=$value[username];
}
echo "<div align=left>
<form action='$url' method='POST'>";
echo "<b>$select_user :</b>&nbsp;<select name='client_id' onChange='this.form.submit();'>
<option value=0>----------</option>\n";
echo "<optgroup label='Active'>";
foreach ($result as $key => $val) {
	if ($val['blocked'] == 0) {
		if (isset($client_id) && $client_id == $val['client_id']) {
			$ch = 'selected';
		} else {
			$ch = '';
		}
		echo "<option value='".$val['client_id']."' $ch>".$val['username']."</option>\n";
	}
}
echo "</optgroup>
	<optgroup label='Suspended (traffic)'>";
unset($key,$val);
foreach ($result as $key => $val) {
	if ($val['blocked'] == 2) {
		if (isset($_POST['client_id']) && $_POST['client_id'] == $val['client_id']) {
			$ch = 'selected';
		} else {
			$ch = '';
		}
		echo "<option value='".$val['client_id']."' $ch>".$val['username']."</option>\n";
	}
}
echo "</optgroup>
<optgroup label='Suspended (expired)'>";
unset($key,$val);
foreach ($result as $key => $val) {
	if ($val['blocked'] == 3) {
		if (isset($_POST['client_id']) && $_POST['client_id'] == $val['client_id']) {
			$ch = 'selected';
		} else {
			$ch = '';
		}
		echo "<option value='".$val['client_id']."' $ch>".$val['username']."</option>\n";
	}
}
echo "</optgroup>";
echo "</select>
<input type='hidden' name='form_sent' value='yes'></form></div>";
if (isset($form_sent) && $form_sent == 'yes') {
	if (empty($client_id) or !isset($client_id)) {
		echo "<div class=errormsg>$no_client_selected</div>";
	} else {
		$client_id = (int) $client_id;
		//-----------------------------------------------------
                $sql['type'] = "SELECT";
                $sql['params'] = "cl.username, (cl.firstname||' '||cl.lastname) AS name, cl.server, cl.blocked, pr.name AS prname, pr.wholesale,pr.product_id, pu.start_date, pu.expire_date";
                $sql['table'] = "purchases AS pu, products AS pr, clients AS cl";
                $sql['where'] = "pu.client_id = '$client_id' AND pu.client_id=cl.client_id AND pu.product_id = pr.product_id AND pr.type = 'plan'";
                $sql['options'] = "";
                $sql['return'] = "struct";
                #$sql['return'] = "query";
	        //------------------------------------------------------
	        #exit($result=sql_exec($sql,$dbh81,'assoc'));
	        if (!$result=sql_exec($sql,$dbh81,'assoc')) {
	                exit($dberror);
	        }
		#exit(print_r($result));
		$result = $result[0];
		$params['sid']=$sid;
		$params['action']='client_login';
		$params['client']=$result['username'];
		$lurl="$baseURL/?".encode_params($params);

		if ($result['blocked'] != 0) {
			if ($result['blocked'] == 2) {
				echo "<p>Client suspended - traffic</p>";
			} elseif ($result['blocked'] == 3) {
				echo "<div align='left'><p>Client suspended - expired</p>";

				$params['sid'] = $sid;
				$params['action'] = "manualUpgrades";

				$urla = "$baseURL/$webDIR/?".encode_params($params);
				echo "<p><a href='$urla'>Renew</a>&nbsp;|&nbsp;<a href='$lurl' target='_blank'>Login</a></p></div>";
			}
		} else {

			$start_date = date("M d, Y",strtotime($result['start_date']));
			$expire_date = date("M d, Y",strtotime($result['expire_date']));
			$wholesale = $result['wholesale'][12]['USD'];
			$user_name = $result['username'];
			$cl_name = $result['name'];
			$cl_server = $result['server'];
			$plan_name = $result['prname'];
			$product_id = $result['product_id'];
			unset($params);
			$params[sid] = $sid;
			$params[action] = "comparePlans";
			$params[current_plan] = $product_id;
			$params['from_page'] = 'moveClient2Plan';
			$compareplanurl = "$baseURL/$webDIR/?".encode_params($params);
			$upgrade_plan = translate('upgrade_pl');
			$user_upgrade = translate('user_upgrade',array('username' => "$user_name"));
			$no_upgrades = translate('no_upgrades',array('username' => "$user_name"));
			$selected_user = translate('selected_user');
			$uname = translate('username');
			$curr_plan = translate('curr_plan');

			$expiration_date_th = translate('expire_date');
			$showdomains = translate('show_domains',array('username' => "$user_name",
									'name' => "$cl_name"));
			echo "<script language='JavaScript'>
			<!--
			var myWin = '';
			function openWin(url, name, properties) {
	    	    if (myWin && myWin.open && !myWin.closed) {
			        myWin.close();
	    	    }
    		    myWin = window.open(url, name, properties);
    		    return false;
        	}
			//-->
			</script>";
			echo "<TABLE width='100%' border='0' cellspacing='0' cellpadding='0'>
			<tr>
				<td valign='top'>";
			// user details & current user plan
			echo "<TABLE width='300' class='bTable' cellspacing=0>
			<tr>
				<th>$selected_user</th>
				<th>$uname</th>
				<th>$curr_plan</th>
				<th>&nbsp;</th>
			</tr>
			<tr>
				<td align='center'><b>$cl_name</b></td>
				<td align='center'><b>$user_name</b></td>
				<td align='center'><b>$plan_name</b></td>
				<td align='center'><b><a href='$lurl' target=_blank>Login</a></b></td>
			</tr>

			<tr>

				<td align='center' colspan='3'><b>$expiration_date_th - $expire_date</b></td>

			</tr>
			</table><br>";
			unset($params);
			$params[sid] = $sid;
			$params[action] = "showDomains";
			$params[client_id] = $client_id;
			$showdomainurl = "$baseURL/$webDIR/?".encode_params($params);
			echo "<center><a href='#' onClick=\"return openWin('$showdomainurl','domain_details','width=850, height=450, scrollbars=yes')\">
			$showdomains</a></center><br><br>";
			print_plan_properties($product_id);
			echo "</td>
			<td valign='top'>";
			// current plan details
			#$current_expire_date = get_plan_expire_date($client_id);
			$period_left = round( (strtotime($expire_date)-time())/(3600*24*31));
			$amount_to_return = $wholesale*$period_left/12;

			// plans to upgrade
		        //-----------------------------------------------------
		                $sql['type'] = "SELECT";
		                $sql['params'] = "product_id,name,wholesale";
		                $sql['table'] = "products";
		                $sql['where'] = "reseller_id='$id' AND type='plan'";
		                $sql['options'] = "";
		                $sql['return'] = "struct";
		        //------------------------------------------------------
		        if (!$result=sql_exec($sql,$dbh81,'assoc')) {
		                exit($dberror);
		        }
			$params[sid] = $sid;
			$params[action] = "moveClient2Plan";
			$url = "$baseURL/$webDIR/?".encode_params($params);
			echo "<script language='JavaScript'>
			<!--
			var baa = new Array();";
			$i=0;
			foreach ($result as $val) {
				echo "baa['".$i."']=".$val['product_id'].";\n";
				$i ++;
			} ?>
			var bavCurrent = null;
			function bafSL(id) {
				if (bavCurrent == id) return;
				document.getElementById('paf_'+id).style.display='block';
				if (bavCurrent != null) document.getElementById('paf_'+bavCurrent).style.display='none';
				document.getElementById("wrapper_" + id).className = "boldMe";
    	       	if (bavCurrent) document.getElementById("wrapper_" + bavCurrent).className = "unBoldMe";

				bavCurrent=id;

			}
			function bafSLS() {
				for (i=0;i<baa.length;i++) {
					if (document.getElementById(baa[i])) {
						if (document.getElementById(baa[i]).checked) {
							document.getElementById('paf_'+baa[i]).style.display='block';
							return;
						}
					}
				}
			}
			//-->
			</script>
			<?
			echo "<script type='text/javascript' src='overlib.js'></script>
			<div id='overDiv' style='position:absolute; visibility:hidden; z-index:1000; background-color: none !important;'></div>
			<TABLE width='250' class='bTable' cellspacing=0>
			<form action='$url' method='POST'>
			<tr>
				<th colspan=2>$user_upgrade</th>
			</tr>";
			$noother=0;
			foreach ($result as $value) {
				if ($value['wholesale'][12]['USD'] > $wholesale) {
					$total_price = sprintf("%01.2f",($value['wholesale'][12]['USD']*$period_left/12 - $amount_to_return));

					$display_mo_price = sprintf("%01.2f",$total_price/$period_left);
					if ($period_left < 1 && $period_left >0) {
							$period_left = sprintf("%01.2f",$period_left);
					}
					$div_content = $period_left;
					if ($period_left == 1) {
						$div_content .= " <a href='#' onmouseover='overlib(\"time remining untill expiration date\",
						CAPTION, \"month\");' onmouseout='return nd();'
						onClick='return false;'>month</a> ";
					} else {
						$div_content .= " <a href='#' onmouseover='overlib(\"time remining untill expiration date\",
						CAPTION, \"months\");' onmouseout='return nd();'
						onClick='return false;'>months</a> ";
					}

					$mess_wp_m = sprintf("%01.2f",$value['wholesale'][12]['USD']/12);
					$tmp = sprintf("%01.2f",$wholesale/12);
					$mesg = "($mess_wp_m x $period_left) - ($tmp x $period_left) = $total_price<br>";
					#$mesg .= "Pokazanata cena se poluchava kato se umnozhi mesecnata cena na izbraniq plan po ostavashtite meseci do iztichane na nastoyashtia plan, i ot neya se izvadi umnozhenata po ostavashtia period (v meseci) mesechna cena na nastoqshtiq plan, koqto e zaplatena pri plashtaneto za nastoqshtiq hosting plan";

					$div_content .= "x <a href='#' onmouseover='overlib(\"$mesg\",
						CAPTION, \"ATTENTION!\");' onmouseout='return nd();'
						onClick='return false;'>\$$display_mo_price</a> =
						<a href='#' onmouseover='overlib(\"total price to be payed\",
						CAPTION, \"Price\");' onmouseout='return nd();'
						onClick='return false;'>\$$total_price USD</a>";
					unset($detailsurl,$params);
					$params['sid'] = $sid;
					$params['action'] = "showPlanDetails";
					$params['rid'] = $id;
					$params['pid'] = $value['product_id'];
					$params['from_page'] = 'moveClient2Plan';
					$params['username'] = $user_name;
					$params['current_plan'] = $product_id;
					$params['product'] = '12'."|".$product_id;
					$params['current_expire'] = $expire_date;
					$params['current_plan_name'] = $plan_name;
					$params['period_left'] = $period_left;
					$params['amount_to_return'] = $amount_to_return;
					$detailsurl = "$baseURL/$webDIR/?".encode_params($params);
					echo "<tr>
					<td width='10'><input type='radio' id='".$value['product_id']."' name='product' value='12|".$value['product_id']."'
						onClick=\"bafSL(this.id);\"></td><td width='240' onClick=\"document.getElementById('".$value['product_id']."').click();\">
							<a href='#' onClick=\"return openWin('$detailsurl','PlanDetails',
							'width=650, height=650, scrollbars=yes');\"><span  id=\"wrapper_".$value['product_id']."\">".
						$value['name']."</span></a><div id='paf_".$value['product_id']."'
						style='display:none'>$div_content</div></td>
					</tr>";
					$noother=1;
				}
			}
			if ($noother == 0) {
				echo "<tr><td colspan=2><b>$no_upgrades</b></tr></form>";
			} else {
				echo "<tr>
				<td colspan=2 align='center'><input type='submit' name='submit' value='$upgrade_plan'>
				<input type='hidden' name='action' value='total'>
				<input type='hidden' name='type' value='plan'>
				<input type='hidden' name='user_name' value='$user_name'>
				<input type='hidden' name='current_expire' value='$expire_date'>
				<input type='hidden' name='current_plan_name' value='$plan_name'>
				<input type='hidden' name='period_left' value='$period_left'>
				<input type='hidden' name='amount_to_return' value='$amount_to_return'>
				<input type='hidden' name='step3' value='785'>

				</td>
				</tr>
				</form>
				";
			}
			echo "</table><br>
			<center><a href='#' onClick=\"return openWin('$compareplanurl','Compare_Plans','width=650, height=550, scrollbars=yes');\">".translate('compare_link')."</a></center>";
			/*if ($noother !=0) {
				echo "<br><br><br><br>
				<center><input type='submit' name='submit' value='$upgrade_plan'></center>
				</form>";
			}*/
			echo "<script language='JavaScript'>
			<!--
			bafSLS();
			//-->
			</script>";
			/* echo "<br><br>
			<TABLE class='bTable' cellspacing=0>
				<tr>
					<th>Bosses' Table</th>
				</tr>
				<tr>
					<td>&nbsp;</td>
				</tr>
			</table>"; */
			echo "</td>
			</tr>
			</table>";
		}
	}
}

// fixes
//------------------------------------------------------
$sql['type']='SELECT';
$sql['params']='product_id, name';
$sql['table']='products';
$sql['where']="reseller_id='$id' AND type='plan'";
$sql['options']='order by seq asc';
$sql['return']='struct';
//------------------------------------------------------
if (!$result=sql_exec($sql,$dbh81,'assoc')) {
	exit("<br><div class=errormsg>ERROR</div><br>");
}

echo "<script language='JavaScript'>
<!--
var myWin = '';


function openWin(url, name, properties) {
 if (myWin && myWin.open && !myWin.closed) {
myWin.close();
 }
 myWin = window.open(url, name, properties);
 return false;
}
function confirm_del(what) {
     var is_confirmed=window.confirm('Are you sure you want to delete '+what+' plan?');
     return is_confirmed;
}
//-->
</script>";
echo "<br><div align='left'>";
echo translate('warning2');
 echo "<br><br><TABLE width='300' class='bTable' cellspacing=0>
 <tr>
    <th>Hosting Plan</th>
    <th>Clients</th>
 </tr>";
 foreach ($result as $value) {
    //------------------------------------------------------
    $sql['type']='SELECT';
    $sql['params']='count(distinct pu.client_id) as count';
    $sql['table']='purchases as pu, products as pr, clients as c';
    $sql['where']="pr.product_id='".$value['product_id']."' and pr.product_id=pu.product_id and pu.client_id=c.client_id and c.blocked=0";
    $sql['options']='group by pu.product_id';
    $sql['return']='struct';
    //------------------------------------------------------
    if (!$result=sql_exec($sql,$dbh81,'assoc')) {
	    $count = '0';
    } else {
	    $count = $result[0]['count'];
    }

    $params[sid] = $sid;
    $params[action] = "showPlanDetails";
    $params[rid] = $id;
    $params[pid] = $value['product_id'];
    $detailsurl = "$baseURL/$webDIR/?".encode_params($params);
    unset($params);
    $params[sid] = $sid;
    $params[action] = "showPlanClientsDetails";
    $params[rid] = $id;
    $params[pid] = $value['product_id'];
    $detailsurl2 = "$baseURL/$webDIR/?".encode_params($params);
    unset($params);



    /*$params[sid] = $sid;
    $params[action] = "managePlans";
    $params[prod_id] = $value['product_id'];
    $params[sub] = 'edit';
    $params[cl_count] = $count;
    $editurl = "$baseURL/$webDIR/?".encode_params($params);
    unset($params);
    $params[sid] = $sid;
    $params[action] = "managePlans";
    $params[prod_id] = $value['product_id'];
    $params[sub] = 'del';
    $params[cl_count] = $count;
    $deleteurl = "$baseURL/$webDIR/?".encode_params($params);
    unset($params);
    if ($count != 0 && !in_array($value['name'],array('Business','Corporate'))) {
	    $ehref="<a href=\"$editurl\">edit</a>";
	    $dhref="<a href=\"$deleteurl\">delete</a>";
    } elseif (in_array($value['name'],array('Business','Corporate'))) {
	    $ehref= '-';
	    $dhref= '-';
    } else {
	    $ehref="<a href=\"$editurl\">edit</a>";
	    $dhref="<a href=\"$deleteurl\" onClick=\"return confirm_del('$value[name]');\">delete</a>";
    }*/

    echo "<tr>
    <td><a href='#' onClick=\"return openWin('$detailsurl','Plan_Details','width=650, height=650, scrollbars=yes');\">".$value['name']."</a></td>";
    if ($count != 0) {
	    echo "<td><a href='#' onClick=\"return openWin('$detailsurl2','Plan_Details','width=850, height=450, scrollbars=yes');\">$count</a></td>";
    } else {
	    echo "<td>$count</td>";
    }
    echo "</tr>";
 }
 echo "</table>";
// fixes
?>